package service

import (
	"fmt"
	"strings"
	"testproject/source/bean"
	"testproject/source/db/sql/mysql"
)

func UserLogin(name, pass string) (user bean.Users, r int) {
	db, err := mysql.GetDBConn()
	defer db.Close()
	if err != nil {
		errorPrint(err)
	}
	rows, err := db.Query("select uid,uname,upass,unick,uportrait from users where uname=? and upass=?", name, pass)
	defer rows.Close()
	if err != nil {
		errorPrint(err)
	}
	var uid int
	var uname, upass, unick, uportrait string
	for rows.Next() {
		err := rows.Scan(&uid, &uname, &upass, &unick, &uportrait)
		if err != nil {
			errorPrint(err)
		}
	}
	strings.EqualFold(name, uname)
	if !strings.EqualFold(uname, "") && !strings.EqualFold(upass, "") && strings.EqualFold(name, uname) && strings.EqualFold(pass, upass) {
		return bean.Users{uid, uname, upass, unick, uportrait}, 1
	}
	return bean.Users{}, 0
}
func GetUser(uid int) string {
	db, err := mysql.GetDBConn()
	defer db.Close()
	if err != nil {
		errorPrint(err)
	}
	v := fmt.Sprintf("%d", uid)
	sql := "select uid,uname,upass,unick,uportrait from users where uid!='" + v + "'"
	list := mysql.GetBySqlToListMap(sql)
	json := mysql.ToJson(list)
	return json
}
func errorPrint(err error) {
	fmt.Println("error=", err)
}
